package com.goldze.mvvmhabit.ui.tab_bar.fragment.chat;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.goldze.mvvmhabit.BR;
import com.goldze.mvvmhabit.R;
import com.goldze.mvvmhabit.app.AppViewModelFactory;
import com.goldze.mvvmhabit.databinding.FragmentChatlistBinding;
import com.goldze.mvvmhabit.ui.vp_frg.UpMsgNum;

import me.goldze.mvvmhabit.base.BaseFragment;

/**
 * Created by goldze on 2018/7/18.
 */

public class ChatListFragment extends BaseFragment<FragmentChatlistBinding, ChatViewModel> {

    private UpMsgNum upmsgnum;

    public ChatListFragment(UpMsgNum upmsgnum) {
        this.upmsgnum = upmsgnum;
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_chatlist;
    }

    @Override
    public ChatViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(ChatViewModel.class);
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.getChatUsers();
    }

    @Override
    public void initViewObservable() {
        //监听下拉刷新完成
        viewModel.uc.finishRefreshing.observe(this, new Observer() {
            @Override
            public void onChanged(@Nullable Object o) {
                //结束刷新
                binding.chatRefreshLayout.finishRefreshing();
            }
        });

        viewModel.numMsg.observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                upmsgnum.upMsgNum();
            }
        });
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (!isVisibleToUser) {
            if (viewModel != null) {
                viewModel.resetItemState();
            }
        }
    }
}
